package com.jiazhong.front.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiazhong.commons.Mapper.BookMapper;
import com.jiazhong.commons.Util.JsonResult;
import com.jiazhong.commons.Util.ResultTool;
import com.jiazhong.commons.bean.Book;
import com.jiazhong.front.service.BookService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public JsonResult findLowerBookData(int row) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String json = ops.get("FRONT_INDEX_LOWER_" + row);
        if (json == null) {
            QueryWrapper<Book> wrapper = new QueryWrapper<>();
            wrapper.orderByAsc("price");
            wrapper.last("limit " + row);
            List<Book> list = list(wrapper);
            json = JSONArray.toJSONString(list);
            ops.set("FRONT_INDEX_LOWER_" + row, json, 1, TimeUnit.DAYS);
            return ResultTool.success(list);
        }
        List<Book> list = JSONArray.parseArray(json, Book.class);
        return ResultTool.success(list);
    }

    @Override
    public JsonResult findFreshBookData(int row) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String json = ops.get("FRONT_INDEX_FRESH_" + row);
        if (json == null) {
            QueryWrapper<Book> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            wrapper.last("limit " + row);
            List<Book> list = list(wrapper);
            json = JSONArray.toJSONString(list);
            ops.set("FRONT_INDEX_FRESH_" + row, json, 1, TimeUnit.DAYS);
            return ResultTool.success(list);
        }
        List<Book> list = JSONArray.parseArray(json, Book.class);
        return ResultTool.success(list);
    }

    @Override
    public JsonResult findCategoryBookMoreData(String categoryId) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String json = ops.get("FRONT_INDEX_MORE_CATEGORY_" + categoryId);
        if (json == null){
            QueryWrapper<Book> wrapper = new QueryWrapper<>();
            wrapper.eq("category_id",categoryId);
            List<Book> list = list(wrapper);
            ops.set("FRONT_INDEX_MORE_CATEGORY_" + categoryId,JSONArray.toJSONString(list),1,TimeUnit.DAYS);
            return ResultTool.success(list);
        }
        List<Book> list = JSONArray.parseArray(json, Book.class);
        return ResultTool.success(list);
    }

    @Override
    public JsonResult findLowerBookMoreData() {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String json = ops.get("FRONT_INDEX_LOWER_MORE");
        if (json == null) {
            QueryWrapper<Book> wrapper = new QueryWrapper<>();
            wrapper.orderByAsc("price");
            List<Book> list = list(wrapper);
            json = JSONArray.toJSONString(list);
            ops.set("FRONT_INDEX_LOWER_MORE", json, 1, TimeUnit.DAYS);
            return ResultTool.success(list);
        }
        List<Book> list = JSONArray.parseArray(json, Book.class);
        return ResultTool.success(list);
    }

    @Override
    public JsonResult findFreshBookMoreData() {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String json = ops.get("FRONT_INDEX_FRESH_MORE");
        if (json == null) {
            QueryWrapper<Book> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            List<Book> list = list(wrapper);
            json = JSONArray.toJSONString(list);
            ops.set("FRONT_INDEX_FRESH_MORE", json, 1, TimeUnit.DAYS);
            return ResultTool.success(list);
        }
        List<Book> list = JSONArray.parseArray(json, Book.class);
        return ResultTool.success(list);
    }

    @Override
    public JsonResult findSearchBook(String name) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String json = ops.get("FRONT_INDEX_SEARCH_" + name);
        if (json == null){
            QueryWrapper<Book> wrapper = new QueryWrapper<>();
            wrapper.like("name",name);
            List<Book> list = list(wrapper);
            ops.set("FRONT_INDEX_SEARCH_" + name,JSONArray.toJSONString(list),1,TimeUnit.DAYS);
            return ResultTool.success(list);
        }
        List<Book> list = JSONArray.parseArray(json, Book.class);
        return ResultTool.success(list);
    }

    @Override
    public JsonResult findSearchCategoryBook(String name, String cid) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String json = ops.get("FRONT_INDEX_SEARCH_CATEGORY_" + name + "_" + cid);
        if (json == null){
            QueryWrapper<Book> wrapper = new QueryWrapper<>();
            wrapper.like("name",name);
            wrapper.eq("category_id",cid);
            List<Book> list = list(wrapper);
            ops.set("FRONT_INDEX_SEARCH_CATEGORY_" + name + "_" + cid,JSONArray.toJSONString(list),1,TimeUnit.DAYS);
            return ResultTool.success(list);
        }
        List<Book> list = JSONArray.parseArray(json, Book.class);
        return ResultTool.success(list);
    }
}